import { message } from 'antd';
import { history } from 'umi';
import { fetAgree, fetrefuse, fetreturn, fetchDetail, fetchStatusChange } from '@/services/flowMine/waitApprovals';
import { getMyattenddetail } from '@/services/attendance/overtime';

export default {
    namespace: 'approvalDetail',
    state: {
        applyDetail: {}, // 申请详情
        applayProcessList: [],
        attenddetail: {}
    },
    effects: {
        *fetchDetailAgree({ payload, callBack }, { call }) {
            const result = yield call(fetAgree, payload);
            if (result) {
                if (result.code === 10000) {
                    message.success(result.msg);
                    if (callBack) callBack(true);
                    history.push('/personal/portal/flow/mine/waitApproval');
                } else {
                    message.error(result.msg);
                    if (callBack) callBack(false);
                }
            }
        },
        *fetchDetailRefuse({ payload }, { call }) {
            const result = yield call(fetrefuse, payload);
            if (result) {
                if (result.code === 10000) {
                    message.success('操作成功');
                    history.push('/personal/portal/flow/mine/waitApproval');
                } else {
                    message.error(result.msg);
                }
            }
        },
        *fetchDetailReturn({ payload }, { call }) {
            const result = yield call(fetreturn, payload);
            if (result) {
                if (result.code === 10000) {
                    message.success('操作成功');
                    history.push('/personal/portal/flow/mine/waitApproval');
                } else {
                    message.error(result.msg);
                }
            }
        },
        *fetchDetail({ payload, success }, { call, put }) {
            yield put({
                type: 'save',
                payload: {
                    applyDetail: {},
                    applayProcessList: []
                }
            });
            const result = yield call(fetchDetail, payload);
            if (result && result.code === 10000) {
                yield put({
                    type: 'save',
                    payload: {
                        applyDetail: result.data
                    }
                });
                // eslint-disable-next-line no-unused-expressions
                success && success(result.data);
            }
            if (result && result.code === 30021) {
                message.error(result.msg);
            }
            if (result && result.code === 20010) {
                message.error(result.msg);
            }
        },
        *statusChange({ payload, success }, { call }) {
            const result = yield call(fetchStatusChange, payload);
            // eslint-disable-next-line no-unused-expressions
            success && success(result.data);
        },
        *getAttendancedetail({ payload }, { call, put }) {
            yield put({
                type: 'save',
                payload: {
                    attenddetail: {}
                }
            });
            const result = yield call(getMyattenddetail, payload);
            if (result.code === 10000) {
                yield put({
                    type: 'save',
                    payload: {
                        attenddetail: result.data.rows
                    }
                });
            }
        }
    },
    reducers: {
        save(state, { payload }) {
            return {
                ...state,
                ...payload
            };
        }
    }
};
